package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
 *
 * @author chopper
 * 同步职位信息
 *
 */
public class SyncHrmJobTitleInfo implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(SyncHrmJobTitleInfo.class);
	private String charset = "utf-8";
	private int timeout = 100000;
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		PropertiesUtil properties = new PropertiesUtil();
		def hrlurl = properties.readValue("Hrmurl");
		log.debug("Hrmurl:"+hrlurl);

		def hrlip = properties.readValue("Hrmip");
		log.debug("Hrmip:"+hrlip);

		def guid=Util.getGuid();
		log.debug("guid:"+guid);
		
		def xmlbody = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
							<SOAP-ENV:Body>
								<m:getHrmJobTitleInfo xmlns:m="http://localhost/services/HrmService">
									<m:in0>"""+hrlip+"""</m:in0>
									<m:in1></m:in1>
									<m:in2></m:in2>
								</m:getHrmJobTitleInfo>
							</SOAP-ENV:Body>
						</SOAP-ENV:Envelope>""";
		//log.debug("调用岗位信息接口发送报文信息："+xmlbody);
		//调用职位信息接口
		def xml=Util.callWebService(hrlurl,xmlbody,charset,'',timeout);
		xml = Util.Transform(xml); //去命令空间
		xml = new XmlParser().parseText(xml); //xml解析
		//log.debug("调用岗位信息接口返回报文信息："+xml);
		def result ="";
		try{
			result =xml.Body.getHrmJobTitleInfoResponse.out.JobTitleBean;
			}catch (Exception e){
				result="";
				log.error("EM:",e);
			};
			log.debug("result："+result);
			log.debug("size"+result.size());
			if(result==null||(result!=null&&result.size()==0)){
				return "{\"code\":\"-1\",\"msg\":\"调用接口返回异常，请查看日志\"}";
			}
		result.each{
			try{
				//log.debug("Deal hrm_group_gw--" + "jobtitleid:"+it._jobtitleid.text()+" departmentid:"+it._departmentid.text()+" fullname:"+it._fullname.text()+" //shortname:"+it._shortname.text()+" showorder:"+it._jobtitleid.text());
				
				//增加不存在的职位
				eliteBean.update("INSERT INTO hrm_group (rec_guid,jobtitle,departmentid,fullname,shortname,showorder,hrm_id,modifydate,type) select newid(),gw.jobtitle,gw.departmentid,gw.fullname,gw.shortname,gw.showorder,'"+guid+"',getdate(),'GW' from (select "+it._jobtitleid.text()+" jobtitle,'"+it._departmentid.text()+"' departmentid,'"+it._fullname.text()+"' fullname,'"+it._shortname.text()+"' shortname,'"+it._jobtitleid.text()+"' showorder) gw LEFT JOIN hrm_group hg ON hg.jobtitle = gw.jobtitle AND hg.type = 'gw' WHERE hg.jobtitle IS NULL","dataSource");
				
				//更新存在的职位
				eliteBean.update("update hrm_group set departmentid ='"+it._departmentid.text()+"',fullname = '"+it._fullname.text()+"',shortname = '"+it._shortname.text()+"',showorder = '"+it._jobtitleid.text()+"',hrm_id ='"+guid+"',modifydate =getdate() where type='gw' and jobtitle ="+it._jobtitleid.text()+"","dataSource");
			}catch (Exception e){
				log.error("EM:",e);
			};
		};
		
		eliteBean.update("delete from hrm_group where type='GW' and hrm_id!='"+guid+"'","dataSource"); //删除职位历史数据,保留本次同步信息
		
		def sqltype = eliteBean.chaxun_sql("select count(*) from hrm_group where type='GW' and hrm_id='"+guid+"'","dataSource"); //判断岗位数据是否正常插入
		if(sqltype==0) {
				return "{\"code\":\"-2\",\"msg\":\"调用职位信息接口返回为空，请查看日志\"}";
			};
			
		//清空职位信息表
		eliteBean.update("truncate table org_jobtitle","dataSource");
		
		//写入新增数据
		eliteBean.update("insert into org_jobtitle(jobtitle_id,jobtitle_desc,showorder,createdate,modifydate) select jobtitle,fullname,showorder,getdate(),getdate() from hrm_group where type='GW'","dataSource");
		return "{\"code\":\"1\",\"msg\":\"执行同步更新成功\"}";
	}
}

